package cn.school.educational.service.impl;

import cn.school.common.exception.ApiException;
import cn.school.educational.dao.TeachingMaterialDao;
import cn.school.educational.dao.TeachingMaterialStoreDetilsDao;
import cn.school.educational.entity.TeachingMaterial;
import cn.school.educational.entity.TeachingMaterialStoreDetils;
import cn.school.educational.service.TeachingMaterialStoreDetilsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TeachingMaterialStoreDetilsServiceImpl extends ServiceImpl<TeachingMaterialStoreDetilsDao, TeachingMaterialStoreDetils> implements TeachingMaterialStoreDetilsService {
    @Autowired
    private TeachingMaterialDao teachingMaterialDao;


    @Override
    @Transactional
    public boolean saveBiz(TeachingMaterialStoreDetils sku) {
        // 库存明细处理（调剂）前
        TeachingMaterial teachingMaterial = this.teachingMaterialDao.selectById(sku.getTemaId());
        sku.setTmsdProcNotCount(teachingMaterial.getTemaStoreAmount()); // 处理前库存

        // 更新教材实时库存;
        int skucount = "1".equals(sku.getTmsdType()) ? sku.getTmsdCount() : - sku.getTmsdCount();
        this.teachingMaterialDao.updateSku(sku.getTemaId(), skucount);

        // 库存明细处理(调剂)后
        teachingMaterial = this.teachingMaterialDao.selectById(sku.getTemaId());
        sku.setTmsdProcAfterCount(teachingMaterial.getTemaStoreAmount());
        if (teachingMaterial.getTemaStoreAmount() < 0 ) {
            throw new ApiException("不允许出库，库存不够！");
        }
        sku.setTemaCode(teachingMaterial.getTemaCode());
        sku.setTemaName(teachingMaterial.getTemaName());
        super.save(sku);

        return true;
    }
}
